import type { DataSourceConfig } from '../types/ScraperConfig.js';

/**
 * Ant Design 生态系统数据源配置
 */
export const ANT_ECOSYSTEM_CONFIGS: DataSourceConfig[] = [
  // Ant Design 基础组件
  {
    id: 'ant-design',
    name: 'Ant Design',
    description: 'Ant Design 基础 UI 组件库',
    baseUrl: 'https://ant.design',
    enabled: true,
    type: 'components',
    scraping: {
      listPage: {
        path: '/components/overview-cn',
        fallbackPaths: ['/components/overview', '/docs/react/introduce-cn'],
        itemSelector: '.components-overview-card, .component-grid-item',
        linkSelector: 'a',
        nameSelector: '.component-grid-title, h3, .title',
        categorySelector: '.component-grid-category'
      },
      detailPage: {
        pathPattern: '/components/{name}-cn',
        titleSelectors: ['h1', '.title', '.component-title'],
        descriptionSelectors: ['.component-description', '.markdown > p:first-child', 'blockquote'],
        codeSelectors: ['.highlight', 'pre code', '.code-box-demo'],
        propsTableSelectors: ['.component-api table', '.api-container table']
      },
      rateLimitDelay: 1000,
      retry: {
        maxRetries: 3,
        retryDelay: 2000
      }
    },
    processing: {
      categoryMapping: {
        'General': '通用',
        'Layout': '布局',
        'Navigation': '导航',
        'Data Entry': '数据录入',
        'Data Display': '数据展示',
        'Feedback': '反馈',
        'Other': '其他'
      },
      defaultDescriptions: {
        'Button': '按钮用于开始一个即时操作',
        'Input': '通过鼠标或键盘输入内容，是最基础的表单域的包装',
        'Select': '下拉选择器',
        'Table': '展示行列数据',
        'Form': '高性能订阅式表单，一套绝佳的数据域管理流程'
      },
      tagRules: [
        {
          type: 'name',
          pattern: /button/i,
          tags: ['button', 'action']
        },
        {
          type: 'name',
          pattern: /form|input|select/i,
          tags: ['form', 'input']
        },
        {
          type: 'name',
          pattern: /table|list/i,
          tags: ['data-display', 'table']
        }
      ],
      validation: {
        requiredFields: ['name', 'description'],
        fieldLimits: {
          name: { min: 1, max: 50 },
          description: { min: 5, max: 500 }
        }
      },
      imports: {
        defaultPath: 'antd',
        namePattern: '{name}',
        isDefault: false
      }
    }
  },

  // Ant Design Pro
  {
    id: 'ant-design-pro',
    name: 'Ant Design Pro',
    description: 'Ant Design Pro 中后台解决方案',
    baseUrl: 'https://pro.ant.design',
    enabled: true,
    type: 'templates',
    scraping: {
      listPage: {
        path: '/docs/getting-started-cn',
        fallbackPaths: ['/docs/getting-started', '/'],
        itemSelector: '.template-card, .demo-card',
        linkSelector: 'a',
        nameSelector: '.card-title, h3',
        categorySelector: '.card-category'
      },
      detailPage: {
        pathPattern: '/docs/{name}',
        titleSelectors: ['h1', '.page-title'],
        descriptionSelectors: ['.page-description', '.markdown > p:first-child'],
        codeSelectors: ['.highlight', 'pre code'],
        propsTableSelectors: ['.api-table table']
      },
      rateLimitDelay: 1500,
      retry: {
        maxRetries: 3,
        retryDelay: 2000
      }
    },
    processing: {
      categoryMapping: {
        'Layout': '布局',
        'Form': '表单',
        'Table': '表格',
        'Chart': '图表'
      },
      defaultDescriptions: {
        'ProLayout': '高级布局组件，提供了标准的中后台布局',
        'ProTable': '高级表格组件，在 Table 基础上增加了搜索、筛选、工具栏等功能'
      },
      tagRules: [
        {
          type: 'name',
          pattern: /pro/i,
          tags: ['pro', 'advanced']
        }
      ],
      validation: {
        requiredFields: ['name', 'description', 'code'],
        fieldLimits: {
          name: { min: 1, max: 50 },
          description: { min: 10, max: 500 }
        }
      },
      imports: {
        defaultPath: '@ant-design/pro-components',
        namePattern: '{name}',
        isDefault: false
      }
    }
  },

  // Pro Components
  {
    id: 'pro-components',
    name: 'Pro Components',
    description: 'Pro Components 高级组件库',
    baseUrl: 'https://procomponents.ant.design',
    enabled: true,
    type: 'components',
    scraping: {
      listPage: {
        path: '/components',
        fallbackPaths: ['/docs/intro'],
        itemSelector: '.component-item, .pro-card',
        linkSelector: 'a',
        nameSelector: '.component-name, h3',
        categorySelector: '.component-category'
      },
      detailPage: {
        pathPattern: '/components/{name}',
        titleSelectors: ['h1', '.component-title'],
        descriptionSelectors: ['.component-intro', '.markdown > p:first-child'],
        codeSelectors: ['.highlight', 'pre code', '.code-demo'],
        propsTableSelectors: ['.api-table table', '.component-api table']
      },
      rateLimitDelay: 1000,
      retry: {
        maxRetries: 3,
        retryDelay: 2000
      }
    },
    processing: {
      categoryMapping: {
        'Form': '表单',
        'Table': '表格',
        'Layout': '布局',
        'Display': '展示'
      },
      defaultDescriptions: {
        'ProForm': '高级表单组件，提供了更简单的表单配置方式',
        'ProTable': '高级表格组件，在 Table 基础上增加了搜索、筛选、工具栏等功能'
      },
      tagRules: [
        {
          type: 'name',
          pattern: /pro/i,
          tags: ['pro', 'advanced']
        }
      ],
      validation: {
        requiredFields: ['name', 'description'],
        fieldLimits: {
          name: { min: 1, max: 50 },
          description: { min: 5, max: 500 }
        }
      },
      imports: {
        defaultPath: '@ant-design/pro-components',
        namePattern: '{name}',
        isDefault: false
      }
    }
  },

  // Ant Design Charts
  {
    id: 'ant-design-charts',
    name: 'Ant Design Charts',
    description: 'Ant Design Charts 数据可视化图表库',
    baseUrl: 'https://ant-design-charts.antgroup.com',
    enabled: true,
    type: 'charts',
    scraping: {
      listPage: {
        path: '/en/examples',
        fallbackPaths: ['/zh/examples', '/examples'],
        itemSelector: '.chart-item, .example-item',
        linkSelector: 'a',
        nameSelector: '.chart-name, h3, .title',
        categorySelector: '.chart-category'
      },
      detailPage: {
        pathPattern: '/en/examples/{name}',
        titleSelectors: ['h1', '.chart-title', '.example-title'],
        descriptionSelectors: ['.chart-description', '.example-intro', '.markdown > p:first-child'],
        codeSelectors: ['.highlight', 'pre code', '.demo-code'],
        propsTableSelectors: ['.api-table table', '.props-table table']
      },
      rateLimitDelay: 1000,
      retry: {
        maxRetries: 3,
        retryDelay: 2000
      }
    },
    processing: {
      categoryMapping: {
        'Line': '折线图',
        'Column': '柱状图',
        'Bar': '条形图',
        'Area': '面积图',
        'Pie': '饼图',
        'Scatter': '散点图'
      },
      defaultDescriptions: {
        'Line': '折线图，用于展示数据在连续区间的变化趋势',
        'Column': '柱状图，用于展示不同分类数据的数值对比',
        'Bar': '条形图，适用于分类较多或文本较长的数据对比',
        'Area': '面积图，用于展示数据的量级大小和变化趋势',
        'Pie': '饼图，用于展示数据的比例关系和整体的组成'
      },
      tagRules: [
        {
          type: 'name',
          pattern: /line|area|column|bar/i,
          tags: ['chart', 'basic']
        },
        {
          type: 'name',
          pattern: /pie|rose|donut/i,
          tags: ['chart', 'proportion']
        }
      ],
      validation: {
        requiredFields: ['name', 'description'],
        fieldLimits: {
          name: { min: 1, max: 50 },
          description: { min: 5, max: 500 }
        }
      },
      imports: {
        defaultPath: '@ant-design/charts',
        namePattern: '{name}',
        isDefault: false
      }
    },
    specific: {
      chartTypes: [
        'Line', 'Column', 'Bar', 'Area', 'Pie', 'Rose', 'Scatter', 'Histogram',
        'DualAxes', 'Mix', 'Facet', 'Sankey', 'Sunburst', 'Treemap', 'Waterfall',
        'Funnel', 'Box', 'Violin', 'Heatmap', 'Density', 'RadialBar', 'Gauge',
        'Bullet', 'WordCloud', 'TinyLine', 'TinyArea', 'TinyColumn', 'RingProgress'
      ]
    }
  },

  // Ant Design Mobile
  {
    id: 'ant-design-mobile',
    name: 'Ant Design Mobile',
    description: 'Ant Design Mobile 移动端组件库',
    baseUrl: 'https://mobile.ant.design',
    enabled: false, // 暂时禁用，可以后续启用
    type: 'components',
    scraping: {
      listPage: {
        path: '/components',
        fallbackPaths: ['/zh/components'],
        itemSelector: '.component-item',
        linkSelector: 'a',
        nameSelector: '.component-name',
        categorySelector: '.component-category'
      },
      detailPage: {
        pathPattern: '/components/{name}',
        titleSelectors: ['h1'],
        descriptionSelectors: ['.component-description'],
        codeSelectors: ['.highlight'],
        propsTableSelectors: ['.api-table table']
      },
      rateLimitDelay: 1000,
      retry: {
        maxRetries: 3,
        retryDelay: 2000
      }
    },
    processing: {
      categoryMapping: {
        'Layout': '布局',
        'Navigation': '导航',
        'Data Entry': '数据录入',
        'Data Display': '数据展示',
        'Feedback': '反馈'
      },
      defaultDescriptions: {},
      tagRules: [
        {
          type: 'name',
          pattern: /.*/,
          tags: ['mobile']
        }
      ],
      validation: {
        requiredFields: ['name', 'description'],
        fieldLimits: {
          name: { min: 1, max: 50 },
          description: { min: 5, max: 500 }
        }
      },
      imports: {
        defaultPath: 'antd-mobile',
        namePattern: '{name}',
        isDefault: false
      }
    }
  }
];

/**
 * 根据ID获取数据源配置
 */
export function getDataSourceConfig(id: string): DataSourceConfig | undefined {
  return ANT_ECOSYSTEM_CONFIGS.find(config => config.id === id);
}

/**
 * 获取所有启用的数据源配置
 */
export function getEnabledDataSources(): DataSourceConfig[] {
  return ANT_ECOSYSTEM_CONFIGS.filter(config => config.enabled);
}

/**
 * 根据类型获取数据源配置
 */
export function getDataSourcesByType(type: string): DataSourceConfig[] {
  return ANT_ECOSYSTEM_CONFIGS.filter(config => config.type === type && config.enabled);
}